#include "framework\PluginDefinition.h"
#include "framework\menuCmdID.h"
#include "app.h"
#include "str.h"
#include "sql/sql.h"
#include <string>
#include <cstdlib>
using namespace std;

//
// The data of Notepad++ that you can use in your plugin commands
// 全局变量
//
extern NppData nppData;

void about()
{
    ::MessageBox(NULL, TEXT("hello你好"), TEXT("hello"), MB_OK);
}

void printLength()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    // 获取的是字节长度
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    ::MessageBox(NULL, to_wstring(length).c_str(), TEXT("文档长度"), MB_OK);
}

void printContent()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    if (length != 0)
    {
        // 取出来的是多字节字符集
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(length + 1), (LPARAM)content);
        // ::MessageBox输出是不行的，因为此时的MessageBox就是MessageBoxW
        // 如果此时的文档是以GBK编码的话，取出用MessageBoxA就可以显示，但是用MessageBoxW就不行了，编译都过不去
        // 该方法只能打印GBK GB2312 ASCII字符集，打印UTF8编码的字符会出现乱码
        // ::MessageBoxA(NULL, content, "文档内容", MB_OK);
        // 窄字符转换为宽字符，然后再用xxxW方法显示
        // mbstate_t state = mbstate_t();
        setlocale(LC_ALL, "chinese");
        size_t len = 1 + mbstowcs(NULL, content, 0);
        ::MessageBox(NULL, to_wstring((int)len).c_str(), TEXT("字符个数"), MB_OK);
        wchar_t wc[len];
        wmemset(wc, 0, len);
        mbstowcs(wc, content, len);
        // mbsrtowcs(wc, &content, 0, &state);
        ::MessageBoxW(NULL, wc, TEXT("文档内容"), MB_OK);
    }
}

void addSingleQuotes()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    if (length != 0)
    {
        // Get content
        // Scintilla返回的多字节字符集
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(length + 1), (LPARAM)content);

        char *res = apple(content, length, "\'", "\'");
        ::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)res);
        // 展示过后要释放掉内存防止内存泄漏
        free(res);
        free(content);
    }
}

void addQuotes()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    if (length != 0)
    {
        // Get content
        // Scintilla返回的多字节字符集
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(length + 1), (LPARAM)content);

        char *res = apple(content, length, "\"", "\"");
        ::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)res);
        free(res);
        free(content);
    }
}

void addCommaEnd()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    if (length != 0)
    {
        // Get content
        // Scintilla返回的多字节字符集
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(length + 1), (LPARAM)content);

        char *res = apple(content, length, "", ",");
        ::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)res);
        free(res);
        free(content);
    }
}

void toSingleLine()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // Get content length
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETTEXTLENGTH, 0, 0);
    if (length != 0)
    {
        // Get content
        // Scintilla返回的多字节字符集
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETTEXT, (WPARAM)(length + 1), (LPARAM)content);

        to_single_line(content);
        ::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)content);
        free(content);
    }
}

void toSql()
{
    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    // 选中的内容的长度
    int length = 0;
    length = ::SendMessage(curScintilla, SCI_GETSELTEXT, 0, 0);
    if (length != 0)
    {
        char *content = (char *)malloc((length + 1) * sizeof(char));
        ::SendMessage(curScintilla, SCI_GETSELTEXT, 0, (LPARAM)content);
        string result = to_sql(string(content));
        write_in_new_docment(result.c_str());
        free(content);
    }
}

void write_in_new_docment(const char *content)
{
    // Open a new document
    ::SendMessage(nppData._nppHandle, NPPM_MENUCOMMAND, 0, IDM_FILE_NEW);

    // Get the current scintilla
    int which = -1;
    ::SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&which);
    if (which == -1)
        return;
    HWND curScintilla = (which == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;

    ::SendMessage(curScintilla, SCI_SETTEXT, 0, (LPARAM)content);
}